package com.lxb.store.controller;

import com.lxb.store.entity.Goods;
import com.lxb.store.entity.User;
import com.lxb.store.service.GoodsService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


/**
 * 单个商品页面展示
 */
@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;



    @GetMapping("/goods")
    public String goodsIndex(@RequestParam(value = "id",required =true) Integer id,
                             Model model) {

        //获取登录用户信息
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        //密码设置为空
        if(loginUser != null){
            loginUser.setPassword(null);
        }

        Goods goods = goodsService.getByID(id);
        //System.out.println(goods);

        List<Goods>recommendsList = goodsService.listGoodsTop(4);

        model.addAttribute("goods",goods);
        model.addAttribute("user",loginUser);
        model.addAttribute("recommendsList",recommendsList);
        return "goods";
    }

}
